import { useEffect, useState } from 'react'
import { ShareInfo } from './domain'
import dayjs from '@/plugins/dayjs'
import { useRequest } from 'ahooks'
import { fetchShare } from '@/api/share'
export const useData = (meetingMoid: string) => {
	const [shareInfo, setShareInfo] = useState<ShareInfo>(new ShareInfo())
	const [errorMsg, setErrorMsg] = useState('')
	const [loaded, setLoaded] = useState(false)
	const { data, loading } = useRequest(() => fetchShare(meetingMoid), {
		refreshDeps: [meetingMoid],
	})
	useEffect(() => {
		if (!loading && data) {
			setLoaded(true)
			if (data.success) {
				let startTime = data.start_time ? dayjs(data.start_time).format('YYYY-MM-DD HH:mm') : ''
				setShareInfo({
					meetingMoid: data.meeting_moid,
					subject: data.subject,
					startTime: startTime,
					duration: data.duration,
					e164: data.e164,
					enableRtc: data.enable_rtc,
					enableWechat: data.enable_wechat,
					allowAnonymousMt: data.allow_anonymous_mt,
					miniAppImgPath: data.mini_app_img_path || '',
				})
			} else {
				setErrorMsg(data.description || '')
			}
		}
	}, [data])
	return { shareInfo, errorMsg, loaded }
}
